package GUIs;

import Configs.Config;
import Controllers.NguoiThueController;
import javax.swing.JOptionPane;

public class f_nhanvien_nguoithue_them extends javax.swing.JFrame {

    private static f_nhanvien_nguoithue_them _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private final NguoiThueController nguoithuecontrol;

    public f_nhanvien_nguoithue_them(Config conn) {
        this.conn = conn;
        this.nguoithuecontrol = new NguoiThueController(this.conn);
        initComponents();
    }

    public static f_nhanvien_nguoithue_them getInstance(Config conn) {
        if (_instance == null) {
            _instance = new f_nhanvien_nguoithue_them(conn);
        }
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnthemnv = new javax.swing.JPanel();
        lbcmnd = new javax.swing.JLabel();
        tfcmnd = new javax.swing.JTextField();
        tfhoten = new javax.swing.JTextField();
        lbhoten = new javax.swing.JLabel();
        lbgioitinh = new javax.swing.JLabel();
        lbngaysinh = new javax.swing.JLabel();
        tfsdt = new javax.swing.JTextField();
        lbsdt = new javax.swing.JLabel();
        lbcongviec = new javax.swing.JLabel();
        cbbgioitinh = new javax.swing.JComboBox();
        btnluu = new javax.swing.JButton();
        btnreset = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        spcongviec = new javax.swing.JScrollPane();
        textareacongviec = new javax.swing.JTextArea();
        lbtieude = new javax.swing.JLabel();
        dpngaysinh = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm người thuê nhà trọ");
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        lbcmnd.setText("CMND:");

        lbhoten.setText("Họ tên:");

        lbgioitinh.setText("Giới tính:");

        lbngaysinh.setText("Ngày sinh:");

        lbsdt.setText("Số điện thoại:");

        lbcongviec.setText("Công việc:");

        cbbgioitinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chọn", "Nam", "Nữ" }));

        btnluu.setText("Lưu");
        btnluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnluuActionPerformed(evt);
            }
        });

        btnreset.setText("Làm lại");
        btnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnresetActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        textareacongviec.setColumns(20);
        textareacongviec.setRows(5);
        textareacongviec.setAutoscrolls(false);
        spcongviec.setViewportView(textareacongviec);

        lbtieude.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbtieude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbtieude.setText("Thêm người thuê");

        javax.swing.GroupLayout pnthemnvLayout = new javax.swing.GroupLayout(pnthemnv);
        pnthemnv.setLayout(pnthemnvLayout);
        pnthemnvLayout.setHorizontalGroup(
            pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemnvLayout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbgioitinh)
                    .addComponent(lbngaysinh)
                    .addComponent(lbsdt)
                    .addComponent(lbcongviec)
                    .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(lbcmnd)
                        .addComponent(lbhoten)))
                .addGap(43, 43, 43)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfcmnd)
                    .addComponent(cbbgioitinh, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tfhoten, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfsdt)
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(spcongviec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dpngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(111, 111, 111))
            .addGroup(pnthemnvLayout.createSequentialGroup()
                .addComponent(lbtieude, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(pnthemnvLayout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(btnluu, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnreset)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnthoat)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnthemnvLayout.setVerticalGroup(
            pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnthemnvLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbtieude, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfhoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbhoten, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbgioitinh)
                    .addComponent(cbbgioitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dpngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbsdt, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfsdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(spcongviec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(lbcongviec, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnluu)
                    .addComponent(btnreset)
                    .addComponent(btnthoat))
                .addGap(89, 89, 89))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnthemnv, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnthemnv, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void btnresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnresetActionPerformed
        this.clearform();
    }//GEN-LAST:event_btnresetActionPerformed

    private void btnluuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnluuActionPerformed
        if (this.nguoithuecontrol.Add(this)) {
            f_nhanvien change = f_nhanvien.getInstance(this.conn, null);
            change.refreshNguoiThue();
            int dialogResult = JOptionPane.showConfirmDialog(this, "Thêm mới người thuê thành công! \nBạn có muốn tiếp tục thêm?\nNhấn yes để tiếp tục, no để thoát khung thêm.", "Warning", JOptionPane.YES_NO_OPTION);
            this.clearform();
            if (dialogResult == JOptionPane.NO_OPTION) {
                this.dispose();
            }
        }
    }//GEN-LAST:event_btnluuActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
      this.btnluu.requestFocus();
    }//GEN-LAST:event_formWindowActivated

    private void clearform() {
        this.tfcmnd.setText(null);
        this.tfcmnd.requestFocus();
        this.tfhoten.setText(null);
        this.cbbgioitinh.setSelectedIndex(0);
        this.dpngaysinh.setDate(new java.util.Date(714718800000L));
        this.tfsdt.setText(null);
        this.textareacongviec.setText(null);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnluu;
    private javax.swing.JButton btnreset;
    private javax.swing.JButton btnthoat;
    public javax.swing.JComboBox cbbgioitinh;
    public org.jdesktop.swingx.JXDatePicker dpngaysinh;
    private javax.swing.JLabel lbcmnd;
    private javax.swing.JLabel lbcongviec;
    private javax.swing.JLabel lbgioitinh;
    private javax.swing.JLabel lbhoten;
    private javax.swing.JLabel lbngaysinh;
    private javax.swing.JLabel lbsdt;
    private javax.swing.JLabel lbtieude;
    private javax.swing.JPanel pnthemnv;
    private javax.swing.JScrollPane spcongviec;
    public javax.swing.JTextArea textareacongviec;
    public javax.swing.JTextField tfcmnd;
    public javax.swing.JTextField tfhoten;
    public javax.swing.JTextField tfsdt;
    // End of variables declaration//GEN-END:variables
}
